package com.one.rope.mvp.web.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.one.rope.mvp.basecore.util.MyBeanUtils;
import com.one.rope.mvp.basecore.util.StringUtils;
import com.one.rope.mvp.web.business.entity.CompanyProject;
import com.one.rope.mvp.web.business.dao.CompanyProjectMapper;
import com.one.rope.mvp.web.business.entity.CompanyProjectPartner;
import com.one.rope.mvp.web.business.service.ICompanyProjectPartnerService;
import com.one.rope.mvp.web.business.service.ICompanyProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.one.rope.mvp.web.common.dto.admin.project.AdminEditProjectDTO;
import com.one.rope.mvp.web.common.dto.admin.project.AdminProjectDetailVO;
import com.one.rope.mvp.web.common.dto.admin.project.AdminProjectListVO;
import com.one.rope.mvp.basecore.bean.AdminBusinessMessage;
import com.one.rope.mvp.basecore.bean.ResponseObject;
import com.one.rope.mvp.basecore.exception.BusinessDataException;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 企业项目表 服务实现类
 * </p>
 *
 * @author wesley
 * @since 2020-02-20
 */
@Service
public class CompanyProjectServiceImpl extends
    ServiceImpl<CompanyProjectMapper, CompanyProject> implements ICompanyProjectService {

  @Autowired
  ICompanyProjectPartnerService companyProjectPartnerService;

  @Override
  public ResponseObject editProject(AdminEditProjectDTO requestDTO) {
    CompanyProject companyProject = new CompanyProject();
    MyBeanUtils.copyPropertiesIgnoreNull(requestDTO, companyProject);
    this.updateById(companyProject);
    return ResponseObject.successObject();
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public ResponseObject deleteProject(Long id) {
    // 删除项目
    this.removeById(id);
    // 删除项目关联的岗位数据
    companyProjectPartnerService.remove(
            new QueryWrapper<CompanyProjectPartner>().eq("project_id", id)
    );
    return ResponseObject.successObject();
  }

  @Override
  public ResponseObject projectDetail(Long id) {
    CompanyProject companyProject = this.getById(id);
    if (null == companyProject) {
      throw new BusinessDataException(AdminBusinessMessage.COMPANY_PROJECT_NOT_EXIST);
    }
    AdminProjectDetailVO resultVO = new AdminProjectDetailVO();
    MyBeanUtils.copyPropertiesIgnoreNull(companyProject, resultVO);
    if (!StringUtils.isNullOrEmpty(resultVO.getProjectSpot())) {
      resultVO.setProjectSpotTagList(CollectionUtils.asList(resultVO.getProjectSpot().split(",")));
    }
    return new ResponseObject(resultVO);
  }

  @Override
  public ResponseObject companyProjectList(Long companyId) {
    List<AdminProjectListVO> resultVO = new ArrayList<>();
    List<CompanyProject> companyProjectList = this.list(
        new QueryWrapper<CompanyProject>().eq("company_id", companyId)
    );
    if (companyProjectList.size() > 0) {
      companyProjectList.stream().forEach(item -> {
        AdminProjectListVO listVO = new AdminProjectListVO();
        MyBeanUtils.copyPropertiesIgnoreNull(item, listVO);
        resultVO.add(listVO);
      });
    }
    return new ResponseObject(resultVO);
  }
}
